import type * as zrender from 'zrender';

/**
 * 根据名称查找组内的矩形元素
 * @param group zrender组对象
 * @param name 矩形元素名称
 * @returns 找到的矩形元素，未找到时返回null
 */
export function findReact(group: zrender.Group, name: string): zrender.Rect | null {
  const rect = group.childOfName(name) as zrender.Rect;
  if (rect) {
    return rect;
  }
  return null;
}

/**
 * 根据名称查找组内的文本元素
 * @param group zrender组对象
 * @param name 文本元素名称
 * @returns 找到的文本元素，未找到时返回null
 */
export function findText(group: zrender.Group, name: string): zrender.Text | null {
  const text = group.childOfName(name) as zrender.Text;
  if (text) {
    return text;
  }
  return null;
}

/**
 * 去重获取唯一人员列表
 * @param {PersonSchedule[]} schedules 人员排班数据
 * @returns {PersonSchedule[]} 去重后的人员列表
 */
export function getUniquePersons(schedules: PersonSchedule[]): PersonSchedule[] {
  const uniquePersonMap = new Map<string, PersonSchedule>();

  schedules.forEach(schedule => {
    const personId = schedule.person.id;
    if (!uniquePersonMap.has(personId)) {
      uniquePersonMap.set(personId, schedule);
    } else {
      // 如果人员已存在，合并排班数据
      const existingSchedule = uniquePersonMap.get(personId)!;
      existingSchedule.shifts = [...existingSchedule.shifts, ...schedule.shifts];
    }
  });

  return Array.from(uniquePersonMap.values());
}

/**
 * 获取去重后的人员数量
 * @param {PersonSchedule[]} schedules 人员排班数据
 * @returns {number} 去重后的人员数量
 */
export function getUniquePersonCount(schedules: PersonSchedule[]): number {
  const uniquePersonIds = new Set<string>();
  schedules.forEach(schedule => {
    uniquePersonIds.add(schedule.person.id);
  });
  return uniquePersonIds.size;
}

/**
 * 随机生成 mock 人员数组
 * @param count 人员数量
 * @returns Person[]
 */
export function mockRandomPersons(count: number): Person[] {
  const names = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十', '郑十一', '王十二'];
  const departments = ['技术部', '运营部', '人事部', '财务部', '市场部'];
  const positions = ['工程师', '高级工程师', '运营专员', 'HR专员', '会计', '经理'];
  return Array.from({ length: count }).map((_, i) => ({
    id: (1000 + i).toString(),
    name: names[i % names.length] + (i >= names.length ? i : ''),
    employeeId: `E${(1000 + i)}`,
    department: departments[Math.floor(Math.random() * departments.length)],
    position: positions[Math.floor(Math.random() * positions.length)],
  }));
}

/**
 * 随机生成 mock 班次数组
 * @param personId 人员ID
 * @param count 班次数量
 * @param startBase 班次起始基准（如todayOffset）
 * @returns Shift[]
 */
export function mockRandomShifts(personId: string, count: number, startBase: number = 0): Shift[] {
  const shiftTypes: Array<{ type: Shift['type']; name: string; color: string }> = [
    { type: 'morning', name: '早班', color: '#4CAF50' },
    { type: 'afternoon', name: '中班', color: '#2196F3' },
    { type: 'night', name: '晚班', color: '#795548' },
    { type: 'rest', name: '休息', color: '#607D8B' },
    { type: 'overtime', name: '加班', color: '#FF9800' },
    { type: 'meeting', name: '会议', color: '#E91E63' },
    { type: 'training', name: '培训', color: '#9C27B0' },
  ];
  let lastStart = startBase;
  return Array.from({ length: count }).map((_, i) => {
    const shift = shiftTypes[Math.floor(Math.random() * shiftTypes.length)];
    const duration = Math.floor(Math.random() * 4) + 2;
    const start = lastStart + Math.floor(Math.random() * 3) + 1;
    lastStart = start + duration;
    return {
      id: `s${personId}_${i}`,
      personId,
      name: shift.name,
      start,
      duration,
      type: shift.type,
      fillColor: shift.color,
    };
  });
}
